package entity;

import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Facade for entity Paciente.
 * 
 * @see entity.Paciente
 * @author MyEclipse Persistence Tools
 */
@Stateless
public class PacienteFacade implements PacienteFacadeLocal,
		PacienteFacadeRemote {
	// property constants
	public static final String IS_TRATAMENTO_MEDICO = "isTratamentoMedico";
	public static final String DOC_APRESENTADO = "docApresentado";
	public static final String NUMERO_DOC = "numeroDoc";
	public static final String ORGAO_EXP = "orgaoExp";
	public static final String SERIE_CART_TRAB = "serieCartTrab";
	public static final String NOME_PACIENTE = "nomePaciente";
	public static final String NOME_MAE = "nomeMae";
	public static final String NOME_PAI = "nomePai";
	public static final String LOCAL_NASC = "localNasc";
	public static final String NACIONALIDADE = "nacionalidade";
	public static final String SEXO = "sexo";
	public static final String ESTADO_CIVIL = "estadoCivil";
	public static final String PROFISSAO = "profissao";
	public static final String NOME_CONJUGE_COMP = "nomeConjugeComp";
	public static final String END_RUA = "endRua";
	public static final String END_RUA_NUMERO = "endRuaNumero";
	public static final String END_COMPLEMENTO = "endComplemento";
	public static final String END_CIDADE = "endCidade";
	public static final String END_BAIRRO = "endBairro";
	public static final String END_ESTADO = "endEstado";
	public static final String END_CEP = "endCep";
	public static final String FONE_RESIDENCIAL = "foneResidencial";
	public static final String FONE_COMERCIAL = "foneComercial";
	public static final String CELULAR = "celular";
	public static final String CONTATO_RECADO = "contatoRecado";
	public static final String NOME_RESP_PACIENTE = "nomeRespPaciente";
	public static final String DOC_RESP_PAC = "docRespPac";
	public static final String NUMERO_DOC_RESP_PACIENTE = "numeroDocRespPaciente";
	public static final String ORGAO_EXP_RESP_PACIENTE = "orgaoExpRespPaciente";
	public static final String SERIE_CART_TRAB_RESP = "serieCartTrabResp";

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * Perform an initial save of a previously unsaved Paciente entity. All
	 * subsequent persist actions of this entity should use the #update()
	 * method.
	 * 
	 * @param entity
	 *            Paciente entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Paciente entity) {
		LogUtil.log("saving Paciente instance", Level.INFO, null);
		try {
			entityManager.persist(entity);
			LogUtil.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			LogUtil.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent Paciente entity.
	 * 
	 * @param entity
	 *            Paciente entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Paciente entity) {
		LogUtil.log("deleting Paciente instance", Level.INFO, null);
		try {
			entity = entityManager.getReference(Paciente.class, entity.getId());
			entityManager.remove(entity);
			LogUtil.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			LogUtil.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved Paciente entity and return it or a copy of it
	 * to the sender. A copy of the Paciente entity parameter is returned when
	 * the JPA persistence mechanism has not previously been tracking the
	 * updated entity.
	 * 
	 * @param entity
	 *            Paciente entity to update
	 * @return Paciente the persisted Paciente entity instance, may not be the
	 *         same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public Paciente update(Paciente entity) {
		LogUtil.log("updating Paciente instance", Level.INFO, null);
		try {
			Paciente result = entityManager.merge(entity);
			LogUtil.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			LogUtil.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public Paciente findById(Integer id) {
		LogUtil.log("finding Paciente instance with id: " + id, Level.INFO,
				null);
		try {
			Paciente instance = entityManager.find(Paciente.class, id);
			return instance;
		} catch (RuntimeException re) {
			LogUtil.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Find all Paciente entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the Paciente property to query
	 * @param value
	 *            the property value to match
	 * @param rowStartIdxAndCount
	 *            Optional int varargs. rowStartIdxAndCount[0] specifies the the
	 *            row index in the query result-set to begin collecting the
	 *            results. rowStartIdxAndCount[1] specifies the the maximum
	 *            number of results to return.
	 * @return List<Paciente> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<Paciente> findByProperty(String propertyName,
			final Object value, final int... rowStartIdxAndCount) {
		LogUtil.log("finding Paciente instance with property: " + propertyName
				+ ", value: " + value, Level.INFO, null);
		try {
			final String queryString = "select model from Paciente model where model."
					+ propertyName + "= :propertyValue";
			Query query = entityManager.createQuery(queryString);
			query.setParameter("propertyValue", value);
			if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
				int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
				if (rowStartIdx > 0) {
					query.setFirstResult(rowStartIdx);
				}

				if (rowStartIdxAndCount.length > 1) {
					int rowCount = Math.max(0, rowStartIdxAndCount[1]);
					if (rowCount > 0) {
						query.setMaxResults(rowCount);
					}
				}
			}
			return query.getResultList();
		} catch (RuntimeException re) {
			LogUtil.log("find by property name failed", Level.SEVERE, re);
			throw re;
		}
	}

	public List<Paciente> findByIsTratamentoMedico(Object isTratamentoMedico,
			int... rowStartIdxAndCount) {
		return findByProperty(IS_TRATAMENTO_MEDICO, isTratamentoMedico,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByDocApresentado(Object docApresentado,
			int... rowStartIdxAndCount) {
		return findByProperty(DOC_APRESENTADO, docApresentado,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByNumeroDoc(Object numeroDoc,
			int... rowStartIdxAndCount) {
		return findByProperty(NUMERO_DOC, numeroDoc, rowStartIdxAndCount);
	}

	public List<Paciente> findByOrgaoExp(Object orgaoExp,
			int... rowStartIdxAndCount) {
		return findByProperty(ORGAO_EXP, orgaoExp, rowStartIdxAndCount);
	}

	public List<Paciente> findBySerieCartTrab(Object serieCartTrab,
			int... rowStartIdxAndCount) {
		return findByProperty(SERIE_CART_TRAB, serieCartTrab,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByNomePaciente(Object nomePaciente,
			int... rowStartIdxAndCount) {
		return findByProperty(NOME_PACIENTE, nomePaciente, rowStartIdxAndCount);
	}

	public List<Paciente> findByNomeMae(Object nomeMae,
			int... rowStartIdxAndCount) {
		return findByProperty(NOME_MAE, nomeMae, rowStartIdxAndCount);
	}

	public List<Paciente> findByNomePai(Object nomePai,
			int... rowStartIdxAndCount) {
		return findByProperty(NOME_PAI, nomePai, rowStartIdxAndCount);
	}

	public List<Paciente> findByLocalNasc(Object localNasc,
			int... rowStartIdxAndCount) {
		return findByProperty(LOCAL_NASC, localNasc, rowStartIdxAndCount);
	}

	public List<Paciente> findByNacionalidade(Object nacionalidade,
			int... rowStartIdxAndCount) {
		return findByProperty(NACIONALIDADE, nacionalidade, rowStartIdxAndCount);
	}

	public List<Paciente> findBySexo(Object sexo, int... rowStartIdxAndCount) {
		return findByProperty(SEXO, sexo, rowStartIdxAndCount);
	}

	public List<Paciente> findByEstadoCivil(Object estadoCivil,
			int... rowStartIdxAndCount) {
		return findByProperty(ESTADO_CIVIL, estadoCivil, rowStartIdxAndCount);
	}

	public List<Paciente> findByProfissao(Object profissao,
			int... rowStartIdxAndCount) {
		return findByProperty(PROFISSAO, profissao, rowStartIdxAndCount);
	}

	public List<Paciente> findByNomeConjugeComp(Object nomeConjugeComp,
			int... rowStartIdxAndCount) {
		return findByProperty(NOME_CONJUGE_COMP, nomeConjugeComp,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByEndRua(Object endRua,
			int... rowStartIdxAndCount) {
		return findByProperty(END_RUA, endRua, rowStartIdxAndCount);
	}

	public List<Paciente> findByEndRuaNumero(Object endRuaNumero,
			int... rowStartIdxAndCount) {
		return findByProperty(END_RUA_NUMERO, endRuaNumero, rowStartIdxAndCount);
	}

	public List<Paciente> findByEndComplemento(Object endComplemento,
			int... rowStartIdxAndCount) {
		return findByProperty(END_COMPLEMENTO, endComplemento,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByEndCidade(Object endCidade,
			int... rowStartIdxAndCount) {
		return findByProperty(END_CIDADE, endCidade, rowStartIdxAndCount);
	}

	public List<Paciente> findByEndBairro(Object endBairro,
			int... rowStartIdxAndCount) {
		return findByProperty(END_BAIRRO, endBairro, rowStartIdxAndCount);
	}

	public List<Paciente> findByEndEstado(Object endEstado,
			int... rowStartIdxAndCount) {
		return findByProperty(END_ESTADO, endEstado, rowStartIdxAndCount);
	}

	public List<Paciente> findByEndCep(Object endCep,
			int... rowStartIdxAndCount) {
		return findByProperty(END_CEP, endCep, rowStartIdxAndCount);
	}

	public List<Paciente> findByFoneResidencial(Object foneResidencial,
			int... rowStartIdxAndCount) {
		return findByProperty(FONE_RESIDENCIAL, foneResidencial,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByFoneComercial(Object foneComercial,
			int... rowStartIdxAndCount) {
		return findByProperty(FONE_COMERCIAL, foneComercial,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByCelular(Object celular,
			int... rowStartIdxAndCount) {
		return findByProperty(CELULAR, celular, rowStartIdxAndCount);
	}

	public List<Paciente> findByContatoRecado(Object contatoRecado,
			int... rowStartIdxAndCount) {
		return findByProperty(CONTATO_RECADO, contatoRecado,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByNomeRespPaciente(Object nomeRespPaciente,
			int... rowStartIdxAndCount) {
		return findByProperty(NOME_RESP_PACIENTE, nomeRespPaciente,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByDocRespPac(Object docRespPac,
			int... rowStartIdxAndCount) {
		return findByProperty(DOC_RESP_PAC, docRespPac, rowStartIdxAndCount);
	}

	public List<Paciente> findByNumeroDocRespPaciente(
			Object numeroDocRespPaciente, int... rowStartIdxAndCount) {
		return findByProperty(NUMERO_DOC_RESP_PACIENTE, numeroDocRespPaciente,
				rowStartIdxAndCount);
	}

	public List<Paciente> findByOrgaoExpRespPaciente(
			Object orgaoExpRespPaciente, int... rowStartIdxAndCount) {
		return findByProperty(ORGAO_EXP_RESP_PACIENTE, orgaoExpRespPaciente,
				rowStartIdxAndCount);
	}

	public List<Paciente> findBySerieCartTrabResp(Object serieCartTrabResp,
			int... rowStartIdxAndCount) {
		return findByProperty(SERIE_CART_TRAB_RESP, serieCartTrabResp,
				rowStartIdxAndCount);
	}

	/**
	 * Find all Paciente entities.
	 * 
	 * @param rowStartIdxAndCount
	 *            Optional int varargs. rowStartIdxAndCount[0] specifies the the
	 *            row index in the query result-set to begin collecting the
	 *            results. rowStartIdxAndCount[1] specifies the the maximum
	 *            count of results to return.
	 * @return List<Paciente> all Paciente entities
	 */
	@SuppressWarnings("unchecked")
	public List<Paciente> findAll(final int... rowStartIdxAndCount) {
		LogUtil.log("finding all Paciente instances", Level.INFO, null);
		try {
			final String queryString = "select model from Paciente model";
			Query query = entityManager.createQuery(queryString);
			if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
				int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
				if (rowStartIdx > 0) {
					query.setFirstResult(rowStartIdx);
				}

				if (rowStartIdxAndCount.length > 1) {
					int rowCount = Math.max(0, rowStartIdxAndCount[1]);
					if (rowCount > 0) {
						query.setMaxResults(rowCount);
					}
				}
			}
			return query.getResultList();
		} catch (RuntimeException re) {
			LogUtil.log("find all failed", Level.SEVERE, re);
			throw re;
		}
	}

}